package com.chezai.client.adapter;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chezai.client.R;
import com.chezai.client.bean.Speech;
import com.chezai.client.utils.SpeechUtils;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechEvent;
import com.iflytek.cloud.SynthesizerListener;
import com.orhanobut.logger.Logger;

import java.util.List;

/**
 * 语音测试Adapter
 * Created by likunyang on 2018/2/1.
 */

public class SpeechTestAdapter extends BaseQuickAdapter<Speech, BaseViewHolder> implements SynthesizerListener {


    private BaseViewHolder tempHolder;

    public SpeechTestAdapter(int layoutResId, @Nullable List<Speech> data) {
        super(layoutResId, data);
        SpeechUtils.getInstance().setSynListener(this);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final Speech item) {
        holder.setText(R.id.tv_msg, item.getMsg());
        final Button btn = holder.getView(R.id.btn_play);
        final TextView tv = holder.getView(R.id.tv_result);
        if (item.isExist()) {
            btn.setText("播放");
            btn.setTag("play");
            tv.setText("语音文件已合成，可点击播放试听");
        } else {
            btn.setText("合成");
            btn.setTag("compose");
            tv.setText("语音文件尚未合成，可点击合成按钮");
        }
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tempHolder = holder;
                if (TextUtils.equals((CharSequence) v.getTag(), "play")) {
                    SpeechUtils.getInstance().playSound(item.getFilePath());
                } else if (TextUtils.equals((CharSequence) v.getTag(), "compose")) {
                    SpeechUtils.getInstance().composedSound(item.getMsg(), item.getFilePath());
                }
            }
        });
    }

    @Override
    public void onSpeakBegin() {
        tempHolder.setText(R.id.btn_play, "播放中");
        tempHolder.getView(R.id.btn_play).setEnabled(false);
    }

    @Override
    public void onSpeakPaused() {
    }

    @Override
    public void onSpeakResumed() {
    }

    @Override
    public void onBufferProgress(int percent, int beginPos, int endPos, String info) {
        // 合成进度
        Logger.t(TAG).d("合成进度：" + percent);
        if (percent < 100) {
            tempHolder.setText(R.id.btn_play, "合成中");
            tempHolder.getView(R.id.btn_play).setEnabled(false);
        } else {
            tempHolder.setText(R.id.tv_result, "语音文件已合成，可点击播放试听");
            tempHolder.getView(R.id.btn_play).setTag("play");
        }
    }

    @Override
    public void onSpeakProgress(int percent, int beginPos, int endPos) {
        // 播放进度
        Logger.t(TAG).d("播放进度：" + percent);
    }

    @Override
    public void onCompleted(SpeechError error) {
        tempHolder.getView(R.id.btn_play).setEnabled(true);
        if (error == null) {
            tempHolder.setText(R.id.btn_play, "播放");
        } else {
            tempHolder.setText(R.id.tv_result, error.getPlainDescription(true));
        }
    }

    @Override
    public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
        // 以下代码用于获取与云端的会话id，当业务出错时将会话id提供给技术支持人员，可用于查询会话日志，定位出错原因
        // 若使用本地能力，会话id为null
        if (SpeechEvent.EVENT_SESSION_ID == eventType) {
            String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID);
            tempHolder.setText(R.id.tv_sid, "会话id:" + sid);
        }
    }

}
